package com.mobileares.midp.widgets.client.page;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-4-20
 * Time: 6:20:44
 * To change this template use File | Settings | File Templates.
 */
public class PagesFactory {
    private static Map pages = new HashMap();

    static {
        pages = new HashMap();
    }

    private static PagesFactory factory = GWT.create(PagesFactory.class);
    private static boolean init = false;

    public static PagesFactory getInstance() {
        return factory;
    }

    public void getPage(String path, PageClient client) {

        client.success((AbstractPage) pages.get(path));

    }

    public void getPage(final String mockPath, final String path, final PageClient client) {
        getPage(mockPath, new PageClient() {
            public void success(AbstractPage page) {
                if (page == null) {
                    AbstractPage oPage = (AbstractPage) pages.get(path);
                    AbstractPage newPage = oPage.getInstance();
                    pages.put(mockPath, newPage);
                    client.success(newPage);
                } else {
                    client.success(page);
                }
            }

            public void failure() {
                client.failure();
            }
        });

    }
}
